package com.blb.community_admin.controller;


import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.SysRole;
import com.blb.community_admin.entity.SysRoleMenu;
import com.blb.community_admin.service.ISysRoleMenuService;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = "角色权限")
@RestController
public class SysRoleMenuController {

    @Resource
    private ISysRoleMenuService sysRoleMenuService;

    @Log(module = "角色权限", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加用户")
    @PostMapping("/roles/addrolesmenus")
    public ResponseResult<String> addRoleMenu( Long roleId,@RequestBody SysRole sysRole){
        System.out.println(roleId+"---"+sysRole);
        for (int i=0 ; i<sysRole.getMenuIds().length ;i++ ){
            sysRoleMenuService.save(new SysRoleMenu(roleId,sysRole.getMenuIds()[i]));
        }
        return  ResponseResult.ok("添加成功");
    }

    @ApiOperation("回显")
    @GetMapping("/roles/getbyroleId/{roleId}")
    public ResponseResult<SysRole> getByRoleId(@PathVariable Long roleId){
        System.out.println(roleId);

        List<SysRoleMenu> list = sysRoleMenuService.getByRoleId(roleId);
        SysRole sysRole = new SysRole();
        Long[] arr = new Long[list.size()];
        for (int i=0;i<list.size();i++){
            if (list.get(i).getMenuId()>1000){
                arr[i]=list.get(i).getMenuId();
            }
        }
        sysRole.setMenuIds(arr);
        System.out.println(sysRole);
        return ResponseResult.ok(sysRole);
    }

    @Log(module = "角色权限", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改角色权限")
    @PostMapping("/roles/updaterolesmenu")
    public ResponseResult<String> updateRolesMenu(@RequestBody SysRole sysRole){
        System.out.println(sysRole);
        sysRoleMenuService.delByRoleId(sysRole.getRoleId());

        for (int i=0 ; i<sysRole.getMenuIds().length ;i++ ){
            sysRoleMenuService.save(new SysRoleMenu(sysRole.getRoleId(),sysRole.getMenuIds()[i]));
        }
        return ResponseResult.ok("修改成功");
    }

}
